package jk.zmn.thread.syn;

/**
 * @author 张梦楠
 * @Title: ${file_name}
 * @Package ${package_name}
 * @Description: ${todo}
 * @date 2018/5/2618:47
 * @blog www.itzmn.com
 *
 *  单例模式三种实现
 *
 */
public class MyJvm {

     public static void main(String[] args) {
         Jvm1 jvm1 = new Jvm1();
         Jvm1 jvm2 = new Jvm1();

         System.out.println(jvm1);
         System.out.println(jvm2);

     }

}

/**
 * 饿汉模式
 * 当类初始化时候就会创建实例
 */
class Jvm1{
    private static Jvm1 jvm1 = new Jvm1();

    public static Jvm1 getInstance(){
        return jvm1;
    }

}

/**
 * 懒汉式，当调用方法时候才会初始化，懒加载
 */
class Jvm2{
    private static Jvm2 jvm2 = null;

    public static Jvm2 getInstance(){
        if (jvm2 == null) {
            jvm2 = new Jvm2();
        }
        return jvm2;
    }

}


/**
 * 双重检查
 *
 */

class Jvm3{
    private static Jvm3 jvm3 = null;

    public static Jvm3 getInstance(){
        if (jvm3 ==null){
            synchronized (Jvm3.class) {
                if (jvm3 == null) {
                    jvm3 = new Jvm3();
                }
            }
        }
        return jvm3;
    }

}

/**
 * 静态内部类
 */
class Jvm4{

    private static class get{
        private static Jvm3 jvm3 = new Jvm3();
    }

    public static Jvm3 getInstance(){
        return get.jvm3;
    }

}